import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;

public class TextInput extends TextBox implements CommandListener 
{
    private static final Command CMD_OK = new Command("OK", Command.OK, 1);
    private static final Command CMD_CANCEL = new Command("Cancel", Command.CANCEL, 1);
    private Table parent;
    private Display display;
	private boolean saveAs = false;
	private boolean setText = false;

    public TextInput(String text, Table parent, Display display) 
	{
        super("Enter Text", text, 150, TextField.ANY);
        this.parent = parent;
        this.display = display;
        addCommand(CMD_OK);
        addCommand(CMD_CANCEL);
        setCommandListener(this);

		setText = true;
    }

    public TextInput(String title, Table parent, Display display,int length) 
	{
        super(title, "", length, TextField.ANY);
        this.parent = parent;
        this.display = display;
        addCommand(CMD_OK);
        addCommand(CMD_CANCEL);
        setCommandListener(this);

		saveAs = true;
    }

    public void commandAction(Command c, Displayable d) 
	{
        if (c == CMD_OK) 
		{
            // update the table's cell and return
		if(setText == true)
		{
            	parent.setText(getString());
		}
		if(saveAs == true)
		{
			parent.saveRecord(getString());
		}
            display.setCurrentItem(parent);
        }
		else if (c == CMD_CANCEL) 
		{
            // return without updating the table's cell
            display.setCurrentItem(parent);
        }
    }
}
